

package com.mt.restaurant.controller.material;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.material.SupplyMaterialEditDto;
import com.mt.restaurant.entity.material.SupplyMaterial;
import com.mt.restaurant.service.material.SupplyMaterialService;


@Api(tags = "供应原材料")
@RestController
@RequestMapping("/api/restaurant/material/SupplyMaterial")
@CrossOrigin(allowCredentials = "true")
public class SupplyMaterialController {
    private static Logger logger = LoggerFactory.getLogger(SupplyMaterialController.class);


    @Autowired
    private SupplyMaterialService supplyMaterialService;

    /**
     * 根据分页参数查询供应原材料集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:view')")
    @ApiOperation("分页查询供应原材料")
    @ApiPageParam
    @PostMapping("/findSupplyMaterials")
    public PageResultDTO findSupplyMaterials(@RequestBody PageDTO pageDTO) {
        return this.supplyMaterialService.findSupplyMaterials(pageDTO);
    }

    /**
     * 根据ID查询指定的供应原材料
     *
     * @param supplyMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:view')")
    @ApiOperation("根据id查询供应原材料")
    @ApiPageParam
    @PostMapping("/findSupplyMaterial")
    public SupplyMaterial findSupplyMaterial(@RequestParam Long supplyMaterialId) {
        return this.supplyMaterialService.findSupplyMaterial(supplyMaterialId);
    }

    /**
     * 根据ID查询指定的供应原材料(包含外键名称)
     *
     * @param supplyMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:view')")
    @ApiOperation("根据ID查询指定的供应原材料(包含外键名称)")
    @PostMapping("/findSupplyMaterialForView")
    public SupplyMaterial findSupplyMaterialForView(@RequestParam Long supplyMaterialId) {
        return this.supplyMaterialService.findSupplyMaterialWithForeignName(supplyMaterialId);
    }

    /**
     * 根据ID查询指定的供应原材料(包含供应原材料和外键名称)
     *
     * @param supplyMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:view')")
    @ApiOperation("根据ID查询指定的供应原材料(包含供应原材料和外键名称)")
    @PostMapping("/findSupplyMaterialForEdit")
    public SupplyMaterialEditDto findSupplyMaterialForEdit(@RequestParam Long supplyMaterialId) {
        SupplyMaterialEditDto supplyMaterialEditDto = new SupplyMaterialEditDto();
        supplyMaterialEditDto.setSupplyMaterial(this.supplyMaterialService.findSupplyMaterialWithForeignName(supplyMaterialId));

        this.prepareSupplyMaterialEditDto(supplyMaterialEditDto);

        return supplyMaterialEditDto;
    }

    /**
     * 根据ID查询指定的供应原材料(只提取ID 和 Name)
     *
     * @param supplyMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:view')")
    @ApiOperation("根据ID查询指定的供应原材料(只提取ID 和 Name)")
    @PostMapping("/findSupplyMaterialsWithIdNameById")
    public SupplyMaterial findSupplyMaterialsWithIdNameById(@RequestParam Long supplyMaterialId) {
        return this.supplyMaterialService.findSupplyMaterialsWithIdNameById(supplyMaterialId);
    }

    /**
     * 根据名称查询供应原材料集合(只提取ID 和 Name)
     *
     * @param supplyMaterialName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:view')")
    @ApiOperation("根据名称查询供应原材料集合(只提取ID 和 Name)")
    @PostMapping("/findSupplyMaterialsWithIdNameByName")
    public List<SupplyMaterial> findSupplyMaterialsWithIdNameByName(@RequestParam String supplyMaterialName) {
        return this.supplyMaterialService.findSupplyMaterialsWithIdNameByName(supplyMaterialName);
    }


    /**
     * 创建新的供应原材料
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:add')")
    @ApiOperation("创建新的供应原材料")
    @PostMapping("/createSupplyMaterial")
    public SupplyMaterialEditDto createSupplyMaterial() {
        SupplyMaterialEditDto supplyMaterialEditDto = new SupplyMaterialEditDto();
        supplyMaterialEditDto.setSupplyMaterial(new SupplyMaterial());

        this.prepareSupplyMaterialEditDto(supplyMaterialEditDto);
        return supplyMaterialEditDto;
    }

    private void prepareSupplyMaterialEditDto(SupplyMaterialEditDto supplyMaterialEditDto) {
    }

    /**
     * 新增保存供应原材料
     *
     * @param supplyMaterial 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:add')")
    @ApiOperation("新增保存供应原材料")
    @PostMapping("/saveSupplyMaterial")
    public SupplyMaterial saveSupplyMaterial(@RequestBody SupplyMaterial supplyMaterial) {
        return this.supplyMaterialService.saveSupplyMaterial(supplyMaterial);
    }

    /**
     * 修改保存供应原材料
     *
     * @param supplyMaterial 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:update')")
    @ApiOperation("修改保存供应原材料")
    @PostMapping("/updateSupplyMaterial")
    public SupplyMaterial updateSupplyMaterial(@RequestBody SupplyMaterial supplyMaterial) {
        return this.supplyMaterialService.updateSupplyMaterial(supplyMaterial);
    }

    /**
     * 根据ID删除供应原材料
     *
     * @param supplyMaterialId ID
     */
    @PreAuthorize("hasAuthority('restaurant:material:SupplyMaterial:remove')")
    @ApiOperation("根据ID删除供应原材料")
    @PostMapping("/deleteSupplyMaterial")
    public void deleteSupplyMaterial(@RequestParam Long supplyMaterialId) {
        this.supplyMaterialService.deleteSupplyMaterial(supplyMaterialId);
    }


}

